<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../css/mui.min.css" rel="stylesheet" />
		<script src="../../js/mui.min.js"></script>
		<style>
			button:disabled {
				background-color: #AAAAAA;
				border-color: #AAAAAA;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav" style="background-color: #498af7;">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: #FFFFFF;"></a>
			<h1 class="mui-title" style="color: #FFFFFF;">更改邮箱</h1>
		</header>
		<div class="mui-content">
			<form class="mui-input-group">
				<div class="mui-input-row">
					<label>邮箱地址</label>
					<input type="email" id="email" class="mui-input-clear mui-input" placeholder="请输入新的邮箱地址" autofocus="autofocus" />
				</div>
			</form>
			<div class="mui-content-padded">
				<button id='okButton' type="button" class="mui-btn mui-btn-block mui-btn-primary" disabled="true">确认</button>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		mui.init()
		var email = document.getElementById("email");
		var okButton = document.getElementById("okButton");

		email.addEventListener('input', function() {
			if(this.value !== "") {
				okButton.removeAttribute('disabled');
			} else {
				okButton.setAttribute('disabled', 'true')
			}
		})
		okButton.addEventListener('tap', function() {
			mui.ajax('http://app.xiaoweicanting.com/hospital.php?m=App&c=login&a=updateemail', {
				data:{
					userid: '4',
					email: email.value
				},
				dataType: 'json',
				type: 'get',
				timeout: 10000, //超时时间设置为10秒；
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'  //修改
				},
				success: function(data) {
					if(data.state === "success") {
						mui.toast(data.msg);
						//操作成功后修改本地存储和个人信息页面数据并返回
						resetData('email', email.value);
						var ws = plus.webview.getWebviewById("personal_info.html");
						mui.fire(ws,'updateData',{target: 'email'});
						mui.back();
					} else if(data.state === "fail") {
						mui.toast(data.msg);
					}
				},
				error: function(xhr, type, errorThrown) {
					//异常处理；
					mui.toast('网络异常');
				}
			});
		})
		
		function resetData(key, newValue) {
			//从storage中取出字符串,将字符串转化为对象,修改后转为字符串存入本地
    		var userData = JSON.parse( plus.storage.getItem("userinfo") );
    		userData[key] = newValue;
			plus.storage.setItem("userinfo", JSON.stringify(userData) );
		}
	</script>

</html>